﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.WebControls;
using System.Web.UI.WebControls;
using Microsoft.SharePoint;
using ParentChildLookup.Tools;
using ParentChildLookup.BO;
using ParentChildLookup.Data;

namespace ParentChildLookup.Controls
{
    public class ChildLookup : BaseLookupField
    {
        private DVDropDownList ddlLookup;
        private RequiredFieldValidator validator;

        protected override void CreateChildControls()
        {
            if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
            {
                ddlLookup = new DVDropDownList();
                ddlLookup.ID = "ChildLookupField";

                if (this.Field.Required)
                {
                    validator = new RequiredFieldValidator();
                    validator.ControlToValidate = ddlLookup.ID;
                    validator.Text = "*";
                }
                ddlLookup.SelectedIndexChanged += new EventHandler(ddlLookup_SelectedIndexChanged);
                this.Controls.Add(ddlLookup);
                if (this.Field.Required)
                    this.Controls.Add(validator);
            }
            else
                base.CreateChildControls();
        }

        void ddlLookup_SelectedIndexChanged(object sender, EventArgs e)
        {
            string key = parentChildLookup.ParentFieldInternalName;
            if (this.ListItem.Properties.ContainsKey(key))
                this.ListItemFieldValue = ddlLookup.SelectedValue;
            else
                this.ListItemFieldValue = null;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (this.ControlMode == SPControlMode.Edit || this.ControlMode == SPControlMode.New)
            {
                if (Page.IsPostBack)
                    this.ListItemFieldValue = ddlLookup.SelectedValue;

                string key = parentChildLookup.ParentFieldInternalName;
                ddlLookup.Enabled = this.ListItem.Properties.ContainsKey(key);

                if (this.ListItem.Properties.ContainsKey(key))
                {
                    string selectedId = this.ListItem.Properties[key].ToString();
                    if (ddlLookup.Attributes["selectedId"] != selectedId)
                    {
                        ddlLookup.Attributes["selectedId"] = selectedId;
                        SPFieldLookup lookupField = (this.List.Fields.GetFieldByInternalName(this.FieldName) as SPFieldLookup);
                        SPList sourceList;
                        using (SPSite site = new SPSite(this.Web.Url))
                        {
                            using (SPWeb web = site.OpenWeb(lookupField.LookupWebId))
                            {
                                sourceList = web.Lists[new Guid(lookupField.LookupList)];
                            }
                        }

                        SPQuery query = new SPQuery();
                        query.Query = string.Format(SPData.Lists.CommonCamlQueries.GetItemByLookup, parentChildLookup.ParentFieldInternalName, selectedId);
                        SPListItemCollection selectedItems = sourceList.GetItems(query);

                        ddlLookup.DataSource = selectedItems;
                        ddlLookup.DataTextField = "Title";
                        ddlLookup.DataValueField = "Id";
                        ddlLookup.DataBind();
                        ddlLookup.Items.Insert(0, new ListItem(string.Empty, string.Empty));

                        if (this.ListItemFieldValue != null)
                        {
                            SPFieldLookupValue field = new SPFieldLookupValue(this.ListItemFieldValue.ToString());
                            try
                            {
                                selectedItems.GetItemById(field.LookupId);

                                SPFieldLookupValue lookupValue = new SPFieldLookupValue(this.ListItemFieldValue.ToString());
                                ddlLookup.SelectedValue = lookupValue.LookupId.ToString();
                            }
                            catch (SPException)
                            {

                            }
                            catch (ArgumentException)
                            {
                            }
                        }
                    }
                }
                else
                {
                    ddlLookup.SelectedValue = string.Empty;
                }
            }
        }
    }
}
